/*
 * Copyright 2012-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.spring.initializr.generator.project;

import io.spring.initializr.generator.project.contributor.ProjectContributor;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 一个默认的{@link ProjectAssetGenerator}实现， 它生成一个包含所有可用的{@link ProjectContributor project
 * contributors}的目录结构。
 * 使用{@link ProjectDirectoryFactory}并根据{@link ProjectDescription}确定要使用的根目录。
 *
 * @author Stephane Nicoll
 */
public class DefaultProjectAssetGenerator implements ProjectAssetGenerator<Path> {

	private final ProjectDirectoryFactory projectDirectoryFactory;

	/**
	 * Create a new instance with the {@link ProjectDirectoryFactory} to use.
	 * @param projectDirectoryFactory the project directory factory to use
	 */
	public DefaultProjectAssetGenerator(ProjectDirectoryFactory projectDirectoryFactory) {
		this.projectDirectoryFactory = projectDirectoryFactory;
	}

	/**
	 * Create a new instance without an explicit {@link ProjectDirectoryFactory}. A bean
	 * of that type is expected to be available in the context.
	 */
	public DefaultProjectAssetGenerator() {
		this(null);
	}

	@Override
	public Path generate(ProjectGenerationContext context) throws IOException {

		// 获取要生成的项目的说明对象
		ProjectDescription description = context.getBean(ProjectDescription.class);

		// 解析项目的目录并创建
		Path projectRoot = resolveProjectDirectoryFactory(context).createProjectDirectory(description);
		Path projectDirectory = initializerProjectDirectory(projectRoot, description);

		// 获取贡献器并向项目指定路径中添加内容（这里猜测是包括示例代码在内的各种资源！！！）
		List<ProjectContributor> contributors = context.getBeanProvider(ProjectContributor.class).orderedStream()
				.collect(Collectors.toList());
		// 这里对实际的工程构建工作，其实就是很多的 ProjectContributor 共同叠加
		for (ProjectContributor contributor : contributors) {
			contributor.contribute(projectDirectory);
		}
		return projectRoot;
	}

	private ProjectDirectoryFactory resolveProjectDirectoryFactory(ProjectGenerationContext context) {
		return (this.projectDirectoryFactory != null) ? this.projectDirectoryFactory
				/*
				 * 如果在创建对象时没有指定projectDirectoryFactory， 那么在解析该对象时会尝试从context容器中去获取
				 * 如果在容器中也找不到这个bean，就会抛出异常：NoSuchBeanDefinitionException
				 */
				: context.getBean(ProjectDirectoryFactory.class);
	}

	private Path initializerProjectDirectory(Path rootDir, ProjectDescription description) throws IOException {
		Path projectDirectory = resolveProjectDirectory(rootDir, description);
		Files.createDirectories(projectDirectory);
		return projectDirectory;
	}

	private Path resolveProjectDirectory(Path rootDir, ProjectDescription description) {
		if (description.getBaseDirectory() != null) {
			return rootDir.resolve(description.getBaseDirectory());
		}
		return rootDir;
	}

}
